<?php
include 'utilities.php';
include 'crawl.php';
Utititles::includeClasses();

$crawl = new Crawl();
$sc_clubs = Sources::$clubs;
$tb_club = new Tb_Clubs();
$tb_club_tour = new Tb_Clubs_Tours();
// Get touraments
$db_tournaments = new Tb_Tournaments();
$tournaments = $db_tournaments->get();

if (!empty($tournaments)) {
    foreach ($tournaments as $tournament) {
        $url = $tournament['source'].$tournament['url_club'];
        $data = $crawl->getPage($url);
        $matches = $crawl->parseData($data, $sc_clubs[0]['pattern']);
        
        if (!empty($matches[1])) {
            foreach ($matches[1] as $name) {
                // Insert into `clubs` table
                $tb_club->insertClub($name);
                $club = $tb_club->get($name);
                
                // Insert into `clubs_tours` table
                $ids = array(
                    'id_club'   => $club['id'],
                    'id_tour'   => $tournament['id']
                );
                $tb_club_tour->insert($ids);
            }
        }
    }
}